﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        //用户未登录，返回登录界面
        if (Session["UserId"] == null)
        {
            Response.Redirect("Login.aspx");
        }
    }

    //用户修改密码
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        //控件全部通过验证
        if (Page.IsValid)
        {
            MyDataDataContext db = new MyDataDataContext();
            //获取要修改的记录对象
            User user = (from r in db.User
                         where r.UserID == Session["UserId"].ToString()
                         select r).FirstOrDefault();

            //用户输入的原密码正确
            if (txtOldPwd.Text.Trim() == user.UserPwd)
            {
              //新密码与原密码一样
              if(txtPwdAgain.Text.Trim() == user.UserPwd)
                {
                   lblMsg.Text = "原密码与新密码一致！";
                }
                else
                {
                    //修改为最新密码
                    user.UserPwd = txtPwdAgain.Text.Trim();
                    db.SubmitChanges();//提交
                    lblMsg.Text = "密码修改成功！";
                }
                
            }
            else
            {
                lblMsg.Text = "原密码不正确！";
            }
            
        }

    }
}